package com.scy.controller;

import com.scy.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/vm2")
public class VMController2 {
    int i = 0;
    /**
     * 经过验证:可以知道静态对象不会被回收.即使这个对象没有被其它地方引用.这个100m空间也不会被释放
     */
    static byte[] bytes1 = new byte[1024 * 1024 * 100];

    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    @RequestMapping("/getvmpara2")
    @ResponseBody
    public User getVMParam() {
        System.out.println("-------getVMParam2方法执行了------");
        long maxMemory = Runtime.getRuntime().maxMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();

        if (i < 1) {
            //第一次访问
            System.out.println("第一次访问该方法");
            // 开启shell脚本
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int j = 1;
                    while (true) {
                        try {
                            byte[] bytes = new byte[1024 * 1024 * 50];

                            SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
                            String now1 = sdf.format(new Date());
                            System.out.println("开始执行脚本了-----------111111111---循环次数:" + j + "----" + now1);
                            Process process = Runtime.getRuntime().exec("sh /root/my1.sh");
                            int exitCode = -999;
                            try {
                                exitCode = process.waitFor();
                                //newLog("processjob 运行后结果："+exitCode);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally {
                                process = null;
                            }
                            System.out.println("shell脚本执行结束了.即将休眠20秒");
                            String now2 = sdf.format(new Date());
                            System.out.println("结束执行脚本了-------------222222222---exitCode=" + exitCode + "---循环次数:" + j + "----" + now2);
                            Thread.sleep(20 * 1000);
                            System.out.println(bytes.length);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        j = j + 1;
                    }
                }
            }).start();

            // 向目标文件写内存状态
            new Thread(new Runnable() {
                String path = "/root/vm_output";

                @Override
                public void run() {
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(path, true);
                        while (true) {
                            long maxMemory1 = Runtime.getRuntime().maxMemory();
                            long totalMemory1 = Runtime.getRuntime().totalMemory();
                            long freeMemory1 = Runtime.getRuntime().freeMemory();

                            SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
                            String now = sdf.format(new Date());

                            String result = "maxMemory:" + maxMemory1 + "----totalMemory:" + totalMemory1 + "----freeMemory:" + freeMemory1 + "----" + now;
                            fos.write(result.getBytes(StandardCharsets.UTF_8));
                            String line_separator = System.getProperty("line.separator");
                            fos.write(line_separator.getBytes(StandardCharsets.UTF_8));
//                            fos.flush();
                            Thread.sleep(2000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (fos != null) {
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
            i = i + 1;
        } else if (i >= 1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int j = 1;
//                    while (true) {
                    try {
                        byte[] bytes = new byte[1024 * 1024 * 10];

                        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
                        String now1 = sdf.format(new Date());
                        System.out.println(Thread.currentThread().getName() + "开始执行脚本了-----------111111111---循环次数:" + j + "----" + now1);
                        Process process = Runtime.getRuntime().exec("sh /root/my1.sh");
                        int exitCode = -999;
                        try {
                            exitCode = process.waitFor();
                            //newLog("processjob 运行后结果："+exitCode);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            process = null;
                        }
                        System.out.println(Thread.currentThread().getName() + " 线程shell脚本执行完成了.即将休眠20秒");
                        String now2 = sdf.format(new Date());
                        Thread.sleep(20 * 1000);
                        System.out.println(Thread.currentThread().getName() + "结束执行脚本了-------------222222222---exitCode=" + exitCode + "---循环次数:" + j + "----" + now2);
                        System.out.println(bytes.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    j = j + 1;
//                }  //这是while(true) 的括弧
                }
            }).start();
        }
        //返回内存状态
        return new User("maxMemory:" + maxMemory + "----totalMemory:" + totalMemory, "freeMemory:" + freeMemory);
    }


    @RequestMapping("/invoke")
    @ResponseBody
    public User invokeJavaFromShell() {

        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
        String now1 = sdf.format(new Date());
        System.out.println(Thread.currentThread().getName() + "开始执行脚本了------invoke方法执行了1111-----" + now1);
        try {
            Process process = Runtime.getRuntime().exec("sh /root/shell/invokejavafromshell.sh");
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String now2 = sdf.format(new Date());
        System.out.println(Thread.currentThread().getName() + "开始执行脚本了------invoke方法执行了222-----" + now2);
        return new User("zs", "shanghai");
    }

    @RequestMapping("/invoke2")
    @ResponseBody
    public User invokeJavaFromShell2() {

        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
        String now1 = sdf.format(new Date());
        System.out.println(Thread.currentThread().getName() + "开始执行脚本了------invoke2方法执行了1111-----" + now1);
        int exitcode=9999;
        try {
            Process process = Runtime.getRuntime().exec("sh /root/shell/2errorinvokejavafromshell.sh");
            exitcode = process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String now2 = sdf.format(new Date());
        System.out.println(Thread.currentThread().getName() + "开始执行脚本了------invoke2方法执行了222-----exitcode="+exitcode+"----" + now2);
        return new User("ls", "beijing");
    }
}
